package flyweight;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 2. 享元工厂
 */
public class FlyweightFactory {
    //容器池
    public Map<String,Flyweight> pool = new ConcurrentHashMap<>();

    public FlyweightFactory(){
        pool.put("A",new ConcreateFlyweight("A"));
        pool.put("B",new ConcreateFlyweight("B"));
        pool.put("C",new ConcreateFlyweight("C"));
    }

    //内部状态查询
    public Flyweight get(String key){
        if (pool.containsKey(key)){
            System.out.println("可复用");
            return pool.get(key);
        }else{
            System.out.println("创建后复用");
            Flyweight flyweight = new ConcreateFlyweight(key);
            pool.put(key,flyweight);
            return flyweight;
        }
    }
}
